﻿using System.ComponentModel;
using IdentityServer4.Models;
using IdentityServer4.Validation;
using System.Security.Claims;
using System.Threading.Tasks;

namespace IdentityServiceForUserInfo
{
    public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
    {
        //根据context.UserName和context.Password与数据库的数据做校验，判断是否合法
        public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            await Task.Run(() =>
            {
                if (context.UserName == "yunZhan" && context.Password == "123456")
                {
                    context.Result = new GrantValidationResult(
                        subject: context.UserName,
                        authenticationMethod: "custom",
                        claims: new Claim[] { new Claim("Name",context.UserName), new Claim("UserId","888"), new Claim("RealName", "云展"), new Claim("Email", "yunZhang@qq.com") });
                }
                else
                {
                    //验证失败
                    context.Result = new GrantValidationResult(TokenRequestErrors.InvalidGrant, "invalidcustom credential");
                }
            });
        }
    }
}
